package com.alibaba.citrus.ots.sales.plan.repository;

import com.alibaba.citrus.ots.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQuerySalesPlanStrategyDetailRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.PageQueryStrategyFactorRequest;
import com.alibaba.citrus.ots.sales.plan.dto.api.request.QuerySalesPlanStrategyDetailRequest;
import com.alibaba.citrus.ots.sales.plan.sdo.SalesPlanStrategyDetailSDO;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.SalesPlanStrategyDetailLoadListRequest;
import com.epoch.app.bcots.sales_plan.salesplanstrategydetail.dto.CreateSalesPlanStrategyDetailResponse;
import com.epoch.app.bcots.sales_plan.salesplanstrategydetail.dto.LoadSalesPlanStrategyDetailRequest;
import com.epoch.app.bcots.sales_plan.salesplanstrategydetail.dto.UpdateSalesPlanStrategyDetailResponse;
import com.epoch.app.bcots.sales_plan.salesplanstrategydetail.model.SalesPlanStrategyDetail;
import com.epoch.app.bcots.sales_plan.salesplanstrategydetail.service.SalesPlanStrategyDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class SalesPlanStrategyDetailRepositoryImpl implements SalesPlanStrategyDetailRepository {

    @Resource
    private SalesPlanStrategyDetailService salesPlanStrategyDetailService;

    /*
     * 分页展示回复策略详情
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public PageResult<List<SalesPlanStrategyDetailSDO>> pageQuery(PageQuerySalesPlanStrategyDetailRequest pageQuerySalesPlanStrategyDetailRequest) {
        SalesPlanStrategyDetailLoadListRequest loadSalesPlanListRequest = salesPlanStrategyQueryDetailRequestConverter(pageQuerySalesPlanStrategyDetailRequest);
        try {
            Result<List<SalesPlanStrategyDetail>> listResult = salesPlanStrategyDetailService.loadSalesPlanStrategyDetailList(loadSalesPlanListRequest);
            List<SalesPlanStrategyDetailSDO> collect = listResult.getResult().stream().map(salesPlanStrategyDetail -> {
                SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO = new SalesPlanStrategyDetailSDO();
                BeanUtils.copyProperties(salesPlanStrategyDetail, salesPlanStrategyDetailSDO);
                salesPlanStrategyDetailSDO.setId(String.valueOf(salesPlanStrategyDetail.getId()));
                salesPlanStrategyDetailSDO.setReplyStrategyId(String.valueOf(salesPlanStrategyDetail.getReplyStrategyId()));
                return salesPlanStrategyDetailSDO;
            }).collect(Collectors.toList());
            return PageResult.of(collect, listResult.getTotal());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
     * 添加计划回复策略详情
     */
    @Override
    @RepositoryInvoker(printLog = true)
    public String createSalesPlanStrategyDetail(SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO) {
        SalesPlanStrategyDetail salesPlanStrategyDetail = new SalesPlanStrategyDetail();
        salesPlanStrategyDetail.setReplyStrategyId(Long.parseLong(salesPlanStrategyDetailSDO.getReplyStrategyId()));
        salesPlanStrategyDetail.setReplyTargetId(salesPlanStrategyDetailSDO.getReplyTargetId());
        PageQueryStrategyFactorRequest pageQueryStrategyFactorRequest = new PageQueryStrategyFactorRequest();
        salesPlanStrategyDetail.setFirstFactorCode(salesPlanStrategyDetailSDO.getFirstFactorCode());
        salesPlanStrategyDetail.setFirstWeightRatio(salesPlanStrategyDetailSDO.getFirstWeightRatio());
        salesPlanStrategyDetail.setSecondFactorCode(salesPlanStrategyDetailSDO.getSecondFactorCode());
        salesPlanStrategyDetail.setSecondWeightRatio(salesPlanStrategyDetailSDO.getSecondWeightRatio());
        salesPlanStrategyDetail.setReplyStrategyType(salesPlanStrategyDetailSDO.getReplyStrategyType());
        salesPlanStrategyDetail.setStatus(1);
        CreateSalesPlanStrategyDetailResponse salesPlanStrategyResponse = salesPlanStrategyDetailService.createSalesPlanStrategyDetail(salesPlanStrategyDetail);
        return salesPlanStrategyResponse.getLastInsertId();
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Boolean editSalesPlanDetailStrategy(SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO) {
        SalesPlanStrategyDetail salesPlanStrategyDetail = new SalesPlanStrategyDetail();
        BeanUtils.copyProperties(salesPlanStrategyDetailSDO, salesPlanStrategyDetail);
        salesPlanStrategyDetail.setId(Long.valueOf(salesPlanStrategyDetailSDO.getId()));
        salesPlanStrategyDetail.setFirstWeightRatio(salesPlanStrategyDetailSDO.getFirstWeightRatio());
        salesPlanStrategyDetail.setSecondWeightRatio(salesPlanStrategyDetailSDO.getSecondWeightRatio());
        UpdateSalesPlanStrategyDetailResponse updateSalesPlanStrategyDetailResponse = salesPlanStrategyDetailService.updateSalesPlanStrategyDetail(salesPlanStrategyDetail);
        return updateSalesPlanStrategyDetailResponse.getCount() > 0;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Boolean deleteSalesPlanDetailStrategyById(QuerySalesPlanStrategyDetailRequest querySalesPlanStrategyDetailRequest) {
        SalesPlanStrategyDetail salesPlanStrategyDetail = new SalesPlanStrategyDetail();
        salesPlanStrategyDetail.setId(Long.parseLong(querySalesPlanStrategyDetailRequest.getId()));
        salesPlanStrategyDetail.setStatus(-1);
        UpdateSalesPlanStrategyDetailResponse updateSalesPlanStrategyDetailResponse = salesPlanStrategyDetailService.updateSalesPlanStrategyDetail(salesPlanStrategyDetail);
        return updateSalesPlanStrategyDetailResponse != null && updateSalesPlanStrategyDetailResponse.getCount() > 0;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public SalesPlanStrategyDetailSDO querySalesPlanStrategyDetail(QuerySalesPlanStrategyDetailRequest planQueryRequest) {
        LoadSalesPlanStrategyDetailRequest loadSalesPlanRequest = loadSalesPlanRequestConverter(planQueryRequest);
        try {
            SalesPlanStrategyDetail salesPlanStrategyDetail= salesPlanStrategyDetailService.loadSalesPlanStrategyDetail(loadSalesPlanRequest);
            SalesPlanStrategyDetailSDO salesPlanStrategyDetailSDO = new SalesPlanStrategyDetailSDO();
            BeanUtils.copyProperties(salesPlanStrategyDetail, salesPlanStrategyDetailSDO);
            return salesPlanStrategyDetailSDO;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private LoadSalesPlanStrategyDetailRequest loadSalesPlanRequestConverter(QuerySalesPlanStrategyDetailRequest planQueryRequest) {
        LoadSalesPlanStrategyDetailRequest loadSalesPlanRequest = new LoadSalesPlanStrategyDetailRequest();
        BeanUtils.copyProperties(planQueryRequest, loadSalesPlanRequest);
        return loadSalesPlanRequest;
    }

    private SalesPlanStrategyDetailLoadListRequest salesPlanStrategyQueryDetailRequestConverter(PageQuerySalesPlanStrategyDetailRequest request) {
        SalesPlanStrategyDetailLoadListRequest loadSalesPlanStrategyRequest = new SalesPlanStrategyDetailLoadListRequest();
        BeanUtils.copyProperties(request, loadSalesPlanStrategyRequest);
        loadSalesPlanStrategyRequest.setReplyStrategyId(Long.parseLong(request.getReplyStrategyId()));
        return loadSalesPlanStrategyRequest;
    }
}
